

import java.util.ArrayList;
import java.util.Random;
import java.net.URLEncoder;
import java.net.URL;

public class GoogleEngine extends SearchEngineBase {
    /** Method to return the URLs from a query to this engine.
     * @param query The query to send to the engine.
     * @param numResults The maximum number of results to return.
     * @return A list of URLs relating to the query.
     */
    public ArrayList<String> doQuery(String query, int numResults) {
	//int pages = (int) Math.ceil((double) (numResults / 10));
	ArrayList<String> results = new ArrayList<String>(numResults);
	String page = null;
	String fixedQuery = null;
	final String ERROR = "Your search - <b>"+query+"</b> - did not match any documents.";
	int previousPos = 0;
	try {
	    fixedQuery = URLEncoder.encode(query, "UTF-8");
	}
	catch(Exception e) {
	    System.out.println("Error in setting up query in UTF-8 for URL in Google " + e.getMessage());
	    e.printStackTrace();
	}
	int index = 0;
	int i = -1;
	//while(i < pages) {
	try {
	    i++;
	    //page = new HTTPPage("http://search.google.com/search?q="+fixedQuery+"&sitesearch=.gov&output=atom&num=" + numResults).getPage();
	    page = new HTTPPage("http://images.google.com/search?q="+fixedQuery+"&sitesearch=.gov&output=atom&num=" + numResults).getPage();
	    if(page.contains(ERROR))
		return results;


	    while(results.size() < numResults) {
		//index = page.indexOf("<div class=lh><a href=", index) + 23;
		//index = page.indexOf("<link rel=\"alternate\" type=\"text/html\" href=\"", index) + 45;
		index = page.indexOf("<h2 class=r><a href=\"",index)+21;

		if (index < previousPos)
		    break;


		previousPos = index;
		//if(index == 44) {
		//  break;
		//}
		//results.add(URLStrip(page.substring(index, page.indexOf(" ", index) - 2)));
		results.add(URLStrip(page.substring(index, page.indexOf("class", index)- 2)));
	    }
	    previousPos = 0;
	}
	/* We get stuck then wait, drop an i and try again. */
	catch(Exception e) {
	    try {
		System.out.println("Error when requesting page " + i + " at google ... trying to sleep ..." + e.getMessage());
		e.printStackTrace();
		Random rnd = new Random();
		Thread.sleep(rnd.nextInt(5000) + 5000);
		return doQuery(query, numResults);
	    }
	    catch(Exception e2) {
		System.out.println("Error in thread sleep in google at page " + i + " ... " + e.getMessage());
		e2.printStackTrace();
		return doQuery(query, numResults);
		//break;
	    }
	    //i--;
	    //continue;
	}
	//}
	return results;
    }
    public String toString() {
	return "Google.";
    }
    public static void main(String[] args) {
	GoogleEngine x = new GoogleEngine();
	ArrayList<String> results = x.doQuery("rca television model f25250bc", 14);
    
	System.out.println("Num of results "+results.size());
    
	if(results.size() < 1)
	    System.out.println("Error");
    	
	for(String result : results) {
	    System.out.println(result);
	}
    }
}
